﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace PeapodCatcher
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Options : Window
    {


        public class SortableObservableCollection<T> : ObservableCollection<T>
        {
            public SortableObservableCollection()
            {
            }
            public SortableObservableCollection(IEnumerable<T> items)
            {
                foreach (var item in items)
                {
                    Add(item);
                }
            }
            public void Sort<TKey>(Func<T, TKey> keySelector)
            {
                InternalSort(Items.OrderBy(keySelector));
            }
            public void Sort<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer)
            {
                InternalSort(Items.OrderBy(keySelector, comparer));
            }
            private void InternalSort(IEnumerable<T> sortedItems)
            {
                var sortedItemsList = sortedItems.ToList();

                foreach (var item in sortedItemsList)
                {
                    Move(IndexOf(item), sortedItemsList.IndexOf(item));
                }
            }
        }

        private Data _db = new Data(Common.DatabasePath);
        private SortableObservableCollection<Feed> _feeds;
        public SortableObservableCollection<Feed> Feeds
        {
            get
            {
                if (_feeds == null)
                {
                    var feedsQuery = from f in _db.Feed orderby f.Title select f;
                    _feeds = new SortableObservableCollection<Feed>(feedsQuery);
                }
                return _feeds;
            }
        }

        protected void EditItem()
        {
        }

        public Options()
        {
            InitializeComponent();
            this.DataContext = Feeds;
        }

        private void AddFeed(object sender, RoutedEventArgs e)
        {
            AddFeed feedForm = new AddFeed();
            feedForm.OnFeedAdded += new AddFeed.FeedAdded(feedForm_OnFeedAdded);
            feedForm.Show();
        }

        void feedForm_OnFeedAdded(object sender, FeedEventArgs e)
        {
            _db.Feed.InsertOnSubmit(e.ModifiedFeed);
            _db.SubmitChanges();
            Feeds.Add(e.ModifiedFeed);
            ReSortList();
            FeedDetail detail = new FeedDetail(e.ModifiedFeed);
            detail.Show();
        }

        private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.Source == null || !(e.Source is ListBoxItem))
            {
                e.Handled = true;
                return;
            }

            ListBoxItem item = e.Source as ListBoxItem;

            FeedDetail detailForm = new FeedDetail(item.Content as Feed);
            detailForm.OnFeedUpdated += new FeedDetail.FeedUpdated(detailForm_OnFeedUpdated);
            detailForm.Show();
            e.Handled = true;
        }

        private void detailForm_OnFeedUpdated(object sender, EventArgs e)
        {
            _db.SubmitChanges();
            ReSortList();
           
        }

        private void ReSortList()
        {
            Feeds.Sort(f => f.Title);
        }




    }
}
